#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <vector>

//#define BLOCK_SIGNAL 2
#define MAX_SIGNUM 31

using namespace std;

//static vector<int> sigarr = {2, 3};
static vector<int> sigarr = {2};


void show_pending(sigset_t& pending)
{
    for(int signo = 1; signo <= MAX_SIGNUM; ++signo)
    {
        if(sigismember(&pending, signo))
        {
            std::cout << "1";
        }
        else cout << "0";
    }
    cout << "\n";

}
static void myhandler(int signo)
{
    cout << signo << "2号信号已经被递达!" << endl;
}

int main()
{
    for(const auto &sig : sigarr) signal(sig, myhandler);

    //1.先尝试屏蔽指定信号
    sigset_t block,oblock, pending;
    //1.1初始化
    sigemptyset(&block);
    sigemptyset(&oblock);
    sigemptyset(&pending);
    //添加要屏蔽的信号
    for(const auto &sig : sigarr) sigaddset(&block, sig);
    //1.3开始屏蔽,设置进内核(进程)
    sigprocmask(SIG_SETMASK, &block, &oblock);

    //2.遍历打印pengding信号集
    int cnt = 10;
    while(true)
    {
        //2.1 初始化
        sigemptyset(&pending);
        //2.2 获取它
        sigpending(&pending);
        //2.3 打印它
        show_pending(pending);
        //3.慢一点
        sleep(1);
        if(cnt-- == 0)
        {
            sigprocmask(SIG_SETMASK, &oblock, &block);//一但对特点信号进行解除屏蔽,一般os要至少立马递达一个信号!
            cout << "恢复对信号的屏蔽, 不屏蔽任何信号\n";
        }
    }
    return 0;
}